package meiju;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;

public enum TestEnum {
//    RED, BLACK, GREEN, WHITE;
    RED("red",1),BLACK("black",2),WHITE("white",3),GREEN("green",4);
    private String name;
    private int key;

    TestEnum(String name, int key) {
        this.name = name;
        this.key = key;
    }
    private static TestEnum getEnumKey(int key){
        for(TestEnum t:TestEnum.values()){
            if(t.key==key){
                return t;
            }
        }
        return null;
    }
    public static void reflectPrivateConstructor() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class<?> c=Class.forName("meiju.TestEnum");
        Constructor<?> declaredConstructor = c.getDeclaredConstructor(String.class, int.class,String.class,int.class);
        declaredConstructor.setAccessible(true);
        TestEnum o = (TestEnum) declaredConstructor.newInstance("粉色", 777,"蓝色",888);
        System.out.println("获得私有的构造函数"+o);


    }

    public static void main(String[] args) throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
//        TestEnum black = TestEnum.RED;
//        switch (black){
//            case RED -> System.out.println("red");
//            case BLACK -> System.out.println("black");
//            case GREEN -> System.out.println("green");
//            case WHITE -> System.out.println("white");
//        }
//        TestEnum[] values = TestEnum.values();
//        for (TestEnum x : values) {
//            System.out.println(x + " " + x.ordinal());
//        }
//        System.out.println("======");
//        System.out.println(TestEnum.valueOf("GREEN"));
//        TestEnum black = TestEnum.BLACK;
//        TestEnum green = TestEnum.GREEN;
//        System.out.println(black.compareTo(green));
//        System.out.println(BLACK.compareTo(GREEN));
//        System.out.println(GREEN.compareTo(BLACK));
//        TestEnum[] values = TestEnum.values();
//        System.out.println(Arrays.toString(values));
//        TestEnum enumKey = getEnumKey(2);
//        System.out.println(enumKey.name);

        reflectPrivateConstructor();


    }
}
